package com.xctech.yace.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.flink.api.java.tuple.Tuple2;

import java.io.Serializable;
import java.util.Arrays;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Auth implements Serializable {

    public static final String KEY = "password";
    public static final String SPLIT = ":";
    public static final Integer MD5LEN = 32;

    private static final long serialVersionUID = 1L;

    //序号
    private Integer ID;

    private String configTag;

    private String cipher;

    private String mixCipher;

    public static String md5(String password){
        return DigestUtils.md5Hex(password);
    }

    public static String encryptMix(String password,String jasyptPw,String split){
        StringBuilder sb = new StringBuilder(md5(password));
        int l = jasyptPw.length();
        for(int i = 0;i < l;i++){
            sb.append(String.valueOf((int)jasyptPw.charAt(i)));
            if(i+1 != l)sb.append(split);
        }
        return sb.toString();
    }

    public static Tuple2<String,String> decryptMix(String cipherMix, Integer size , String split){
        String cipher = cipherMix.substring(0,size);
        String other = cipherMix.substring(size,cipherMix.length());
        String[] strings = other.split(split);
        StringBuilder sb = new StringBuilder();
        Arrays.stream(strings).forEach(s -> sb.append((char)Integer.parseInt(s)));
        return Tuple2.of(cipher,sb.toString());
    }

}
